package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class MainActivity extends AppCompatActivity {

    private LocalBroadcastReceiver localBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册本地广播接收器
        localBroadcastReceiver = new LocalBroadcastReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(
            localBroadcastReceiver,
            new IntentFilter("com.example.CUSTOM_LOCAL_BROADCAST")
        );

        // 标准广播发送按钮
        Button btnStandard = findViewById(R.id.btn_standard);
        btnStandard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.CUSTOM_STANDARD_BROADCAST");
                sendBroadcast(intent);
                Toast.makeText(MainActivity.this, "标准广播已发送", Toast.LENGTH_SHORT).show();
            }
        });

        // 有序广播发送按钮
        Button btnOrdered = findViewById(R.id.btn_ordered);
        btnOrdered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.CUSTOM_ORDERED_BROADCAST");
                sendOrderedBroadcast(intent, null);
                Toast.makeText(MainActivity.this, "有序广播已发送", Toast.LENGTH_SHORT).show();
            }
        });

        // 本地广播发送按钮
        Button btnLocal = findViewById(R.id.btn_local);
        btnLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.CUSTOM_LOCAL_BROADCAST");
                LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(intent);
                Toast.makeText(MainActivity.this, "本地广播已发送", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销本地广播接收器
        if (localBroadcastReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(localBroadcastReceiver);
        }
    }

    // 本地广播接收器（内部类）
    public static class LocalBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "接收到本地广播", Toast.LENGTH_SHORT).show();
        }
    }
}